home *** CD-ROM | disk | FTP | other *** search
- Path: isonews.bbn.hp.com!hpbblb!news
- From: Matthias Dittrich <matti>
- Newsgroups: comp.lang.c
- Subject: Re: Why doesn't this work?
- Date: 5 Mar 1996 14:41:17 GMT
- Organization: Hewlett-Packard Co.
- Message-ID: <4hhjqd$ki1@hpbblb.bbn.hp.com>
- References: <1996Mar4.161412.137442@forest>
- NNTP-Posting-Host: trabant.bbn.hp.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
- X-URL: news:1996Mar4.161412.137442@forest
-
- ebromber@forest.drew.edu wrote:
- >Does anyone know why this password program doesn't work properly? And if
- >you do know the problem how can I fix it? It rejects every password including
- >the real password. The program was compiled using a MS-DOS compiler.
- >Thanks in advance.
- >ebromber@drew.edu
- >
- >main();
- >{ char real[4];
- > char pass[100];
- > int count=0;
- > int i, error;
- > char c;
- > real[0]='j';real[1]='e';real[2]='r';real[3]='k';
- > printf("PASSWORD: ");
- > fflush(stdout);
- > while (c=getch() !='\n')
- Cosider the precedence order of operators. Here first getch() !='\n' will be
- evaluated, then assigned to c. So c contains 0 or 1.
- What you want is:
- while ((c=getch()) !='\n')
-
- > { count++;
- > pass[count]=c;
- The first value of c will be assigned to pass[1] if you are incrementing
- count before this statement.
-
- > putch('*');
- > }
- > if (count!=4) { printf("\nWRONG PASSWORD\n"); main();}
- > error=0;
- > for (i=0; i<4; i++)
- > if (real[i]=pass[i]) error++;
- You should increase the error counter if the characters are not equal:
- if (real[i] != pass[i]) error++;
- ^^
- Otherwise it will be incremented if the value of pass[i] is not zero.
-
- > if (error>0) {printf("\nWRONG PASSWORD\n"); main();}
- >}
- >
- At all recursion seems not a good solution in this case. I would prefer
- a loop construct using while.
-
- Good luck,
- Matthias
-
-